package jp.sourceforge.akjrcp.text_douga.parser;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.sourceforge.akjrcp.util.PathnameUtils;
import jp.sourceforge.akjrcp.util.YoutubeUtils;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ParserUtils {
	public static Log log = LogFactory.getLog(ParserUtils.class);
	
public static final int FILE_UNKNOWN=0;
public static final int FILE_VIDEO=1;
public static final int FILE_AUDIO=2;
public static final int FILE_DIRECTORY=3;//image list
public static final int FILE_VIDEO_AUDIO=4;
public static final int FILE_IMAGE=5;
public static final int FILE_SUBTTILE=6;
//public static final int FILE_SUBTTILE=5;

public static int toColor(String hex){
	String h=hex;
	if(h.startsWith("#")){
		h=hex.substring(1);
	}
	return Integer.parseInt(h, 16);
}
public static String toFilePath(File baseDir,String path){
	return PathnameUtils.createPath(baseDir.getAbsolutePath()+"/",path);
}
public static DataLine[] parseDataLines(File baseDir,String text){
	List<DataLine> dataLines=new ArrayList<DataLine>();
	
	text=StringUtils.replace(text, "\r\n", "\n");
	text=StringUtils.replace(text, "\r", "\n");
	
	String lines[]=text.split("\n");
	for (int i = 0; i < lines.length; i++) {
		//comment
		if(lines[i].startsWith(";")){
			dataLines.add(new CommentLine(lines[i]));
		}else if(lines[i].startsWith("#")){
			//#color or params
			if(isColorText(lines[i].substring(1))){//#ffffff
				int color=toColor(lines[i]);
				dataLines.add(new ColorLine(lines[i],color));
			}else{
				//property
				String k_v[]=lines[i].substring(1).split(",");
				Map<String,String> map=new HashMap<String,String>();
				for (int j = 0; j < k_v.length; j++) {
					String[] kv=k_v[j].split("=");
					if(kv.length==2){
						putMap(map,kv[0], kv[1]);
					}else if(kv.length==1){
						map.put(kv[0], "1");
					}else{
						//TODO exception
						log.warn("invalid params:"+lines[i]);
					}
				}
				dataLines.add(new PropertyLine(lines[i],map));
			}
		}else{
			boolean wpath=PathnameUtils.isWindowsRootPath(lines[i]);
			if(lines[i].equals("")){
				dataLines.add(new EmptyLine());
			}
			else if(wpath){
				File tmp=new File(lines[i]);
				if(tmp.exists()){
					dataLines.add(new FileLine(lines[i],tmp.getAbsolutePath()));
				}else{
					//
					log.warn("not found:"+lines[i]);
					dataLines.add(new TextLine(lines[i]));
				}
			}else{
				if(isURLLine(lines[i])){
					URLLine urlLine=new URLLine(lines[i]);
					dataLines.add(urlLine);
				}else{
				
				String mixed=toFilePath(baseDir,lines[i]);
				log.info("mixed:"+mixed);
				File tmp=new File(mixed);
				if(tmp.exists()){
					dataLines.add(new FileLine(lines[i],tmp.getAbsolutePath()));
				}else{
					//
					//log.warn("not path");
					dataLines.add(new TextLine(lines[i]));
				}
				}
			}
		}
	}
	
	return dataLines.toArray(new DataLine[dataLines.size()]);
}

//fix values
public static void putMap(Map<String,String> map,String key,String value){
	key=key.toLowerCase();
	if(key.equals("loop")){
		if(value.toLowerCase().equals("forever")){
			value="-1";//forever value;
		}
	}
	map.put(key,value);
}
private static boolean isURLLine(String line){
	//just support youtube
	if(YoutubeUtils.isYoutubeURL(line)){
		return true;
	}
	if(line.startsWith("http://")){
	for (int i = 0; i < supportedDownloads.length; i++) {
		if(line.toLowerCase().endsWith("."+supportedDownloads[i])){
			return true;
		}
	}	
	}
	return false;
}
private static String[] supportedDownloads={"mp3","wav","ogg","jpg","png","gif","flv","avi","wmv","mpg"};
public static boolean isColorText(String text){
if(text==null){
	return false;
}
if(text.startsWith("#")){
	text=text.substring(1);
}

if(text.length()!=6 ){
	return false;
}
char cchar[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','d','f'};
String test=text.toLowerCase();
for (int i = 0; i < text.length(); i++) {
	boolean much=false;
	for (int j = 0; j < cchar.length; j++) {
		if(test.charAt(i)==cchar[j]){
			much=true;
		}
	}
	if(!much){
		return false;
	}
}
return true;
}

public static final int getFileType(String path){
	if(path==null){
		return FILE_UNKNOWN;
	}
	int type=FILE_UNKNOWN;
	String imgs[]={"png","gif","jpg","jpeg","bmp"};
	String videos[]={"avi","flv","mpg","vob","wmv","mp4"};
	String audios[]={"mp3","wav","aac","wma","mp2","ogg"};
	String subtitle[]={"srt"};
	
	for (int i = 0; i < imgs.length; i++) {
		if(path.toLowerCase().endsWith("."+imgs[i])){
			return FILE_IMAGE;
		}
	}
	for (int i = 0; i < videos.length; i++) {
		if(path.toLowerCase().endsWith("."+videos[i])){
			return FILE_VIDEO;
		}
	}
	
	for (int i = 0; i < audios.length; i++) {
		if(path.toLowerCase().endsWith("."+audios[i])){
			return FILE_AUDIO;
		}
	}
	
	for (int i = 0; i < subtitle.length; i++) {
		if(path.toLowerCase().endsWith("."+subtitle[i])){
			return FILE_SUBTTILE;
		}
	}
	
	if(new File(path).isDirectory()){
		return FILE_DIRECTORY;
	}
	return type;
}

}
